home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10741 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q about the float point format......
  5. Date: 19 Mar 1996 09:54:46 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4imsd6INNrle@keats.ugrad.cs.ubc.ca>
  8. References: <s3032089.15.314E68DD@sparc13.ncu.edu.tw> <19MAR199607404893@erich.triumf.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <19MAR199607404893@erich.triumf.ca>,
  12. P.Bennett <bennett@erich.triumf.ca> wrote:
  13.  >In article <s3032089.15.314E68DD@sparc13.ncu.edu.tw>, s3032089@sparc13.ncu.edu.tw (Alexander PeaceLand) writes...
  14.  >> 
  15.  >>   Could I dynamicly set the digits after the float POINT?
  16.  >> 
  17.  >>   In other word... I use printf to print the float point number
  18.  >>                    like 123.456789 but in the other time i only 
  19.  >>                    want to print 123.456 or 123.4 
  20.  >>                    How shall I do? Thanks! :)
  21.  >
  22.  >Read the printf() man pages about width and precision  specifiers.
  23.  >Try something like:
  24.  >    float f = 123.45678;
  25.  >    printf("%6.2f", f);
  26.  >Should print 123.46
  27.  
  28. He wants to do it dynamically. In that case you can use the * specifier to
  29. indicate that the width or precision comes from an int argument rather than
  30. from the format string.
  31.  
  32. If you specify a field width or precision with a *, the width and precision
  33. arguments to convert must appear in that order, _before_ the value argument
  34. converted.
  35.  
  36.     printf("%*.*f",8,2,1.23);
  37. -- 
  38.  
  39.